<?php 
namespace Modules\AuthAdmin\Http\Controllers\v1;
use Illuminate\Http\Request;
use Modules\AuthAdmin\Http\Requests\CommonIdRequest;
use Modules\AuthAdmin\Http\Requests\CommonStatusRequest;
use Modules\AuthAdmin\Http\Requests\AuthOpenRequest;
use Modules\AuthAdmin\Http\Requests\CommonSortRequest;
use Modules\AuthAdmin\Models\AuthRule as AuthRuleModel;
class AuthRuleController extends CommonController
{
	/**
	 * @name  权限列表
	 * @method  GET	
	 * @return  JSON
	 */ 
    public function index(Request $request)
    {
		return (new AuthRuleModel())->list_data($request->all());
    }
	/**
	 * @name  添加
	 * @method  POST	
	 * @param  pid int 父级ID	
	 * @param  level int 级别	
	 * @param  name string 权限名称	
	 * @param  href string 路由地址	
	 * @param  icon string 图标名称	
	 * @param  sort int 排序	
	 * @return  JSON
	 */
    public function store(Request $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_create([
			'pid'=>$data['pid'],
			'level'=>$data['level'],
			'name'=>$data['name'],
			'href'=>$data['href'],
			'icon'=>$data['icon'],
			'sort'=>$data['sort'],
		]);
    }
	/**
	 * @name  添加子级
	 * @method  POST	
	 * @param  pid int 父级ID	
	 * @param  level int 级别	
	 * @param  name string 权限名称	
	 * @param  href string 路由地址	
	 * @param  icon string 图标名称	
	 * @param  sort int 排序	
	 * @return  JSON
	 */
    public function create_sublevel(Request $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_create([
			'pid'=>$data['pid'],
			'level'=>$data['level'],
			'name'=>$data['name'],
			'href'=>$data['href'],
			'icon'=>$data['icon'],
			'sort'=>$data['sort'],
		]);
    }
	/**
	 * @name  编辑页面
	 * @method  GET	
	 * @param  id int 权限id	
	 * @return  JSON
	 */
    public function edit(CommonIdRequest $request)
    {	
		$AuthRuleModel = new AuthRuleModel();
		return $AuthRuleModel->api_success([
			'data'=>$AuthRuleModel->select('id','name','href','icon','sort')->find($request->get('id'))
		]);
    }
	/**
	 * @name  编辑提交
	 * @method  PUT	
	 * @param id int 权限id
	 * @param  name string 权限名称	
	 * @param  href string 路由地址	
	 * @param  icon string 图标名称	
	 * @param  sort int 排序	
	 * @return  JSON
	 */ 
    public function update(Request $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_update($data['id'],[
			'name'=>$data['name'],
			'href'=>$data['href'],
			'icon'=>$data['icon'],
			'sort'=>$data['sort'],
		]); 
    }
	/**
	 * @name  菜单状态
	 * @method  PUT	
	 * @param  id int 权限id
	 * @param  status int 状态（0或1）
	 * @return  JSON
	 */ 
    public function status(CommonStatusRequest $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_status_update($data['id'],['status'=>$data['status']]); 
    }
	/**
	 * @name  是否验证权限
	 * @method  PUT	
	 * @param  id int 权限id
	 * @param  auth_open int 状态（0或1）
	 * @return  JSON
	 */ 
    public function auth_open(AuthOpenRequest $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_status_update($data['id'],['auth_open'=>$data['auth_open']]); 
    }
	/**
	 * @name  排序
	 * @method  PUT	
	 * @param  id int 权限id
	 * @param  sort int 排序
	 * @return  JSON
	 */ 
    public function sorts(CommonSortRequest $request)
    {
		$data = $request->all();
		return (new AuthRuleModel())->common_sort_update($data['id'],['sort'=>$data['sort']]); 
    }
	
	/**
	 * @name  删除
	 * @method  DELETE	
	 * @param  id int 权限id
	 * @return  JSON
	 */
    public function destroy(CommonIdRequest $request)
    {
		$AuthRuleModel = new AuthRuleModel();
		return $AuthRuleModel->common_destroy($AuthRuleModel->ids($request->get('id')));
    }
}
